<?php

namespace app\admin\controller;
use think\Db;

class Position extends Base{

   /*
    * 推荐位列表
    */
   public function index(){
       $list = Db::name('position')->paginate(12);
       $this->assign('list', $list);
        return $this->fetch();
}
 /*
  * 添加推荐位
  */
    public function addPosition(){
        if(IS_POST){
            $article  = I("post.");

            if(empty($article['add_time'])){
                $article['add_time'] =  time();
            }else{
                $article['add_time'] =  strtotime($article['add_time']);
            }
            $result = Db::name('position')->insert($article );

            $data = $result ? array('status' => 1, 'msg' => '添加成功')
                            : array('status' => 0, 'msg' => '添加失败');

                  ajaxReturn($data);
        }else {
            return $this->fetch('add_position');
        }
    }
    /*
     * 修改推荐位
     */
    public function editPosition(){
        if(IS_GET){
            $po_id = I('get.po_id');
            $info = Db::name('position')->where(array('po_id' => $po_id))->find();
            $this->assign('info', $info);
            return $this->fetch('edit_position');
        }
        if(IS_AJAX){
            $po_id = I('get.po_id');
            $position = I('post.');
            $result =  Db::name('position')->where(array('po_id' => $po_id))->update($position);

            $data = $result ? array('status' => 1, 'msg' => '修改成功')
                            : array('status' => 0, 'msg' => '修改失败');

            return ajaxReturn($data);
        }
    }

    /*
     * 删除推荐位
      */
    public function delPosition(){
        if(IS_AJAX){
            $po_id = I('get.po_id');
            $result = Db::name('position')->where(array('po_id' => $po_id))->delete();

            $data = $result ? array('status' => 1, 'msg' => '删除成功')
                            : array('status' => 0, 'msg' => '删除失败');

                         ajaxReturn($data);
        }
    }
}